<?php

namespace App\Exports;

use App\Http\Common\Globals;
use App\Http\Controllers\Admin\costManagement\InFeeBillController;
use Maatwebsite\Excel\Concerns\FromArray;

/**
 * 账单导出
 * Class BillsExport
 * @package App\Exports
 */
class BillsExport implements FromArray
{
    public $request = null;
    public $column = [
        'group_name'=>'项目分组名称',
        'name'=>'名称',
        'hname'=>'住户名称',
        'code'=>'账单编号',
        'aname'=>'小区名称',
        'bname'=>'楼宇名称',
        'uname'=>'单元名称',
        'rname'=>'房间名称',
        'bill_name'=>'账单名称',
        'paid_at'=>'支付时间',
        'count_style'=>'支付方式',
        '`year_month`'=>'账单周期',
        'start_time'=>'开始时间',
        'end_time'=>'截止时间',
        'number'=>'数量（个）',
        'round(real_price/100,2) real_price'=>'单价（元）',
        'round(should_in/100,2) should_in'=>'金额（元）',
        'round(should_in/100,2) input_should_in'=>'实际需交金额（元）',
        'pay_style'=>'支付方式',
        '(case if_payment
            when 1 then "已支付"
            when -1 then "未支付"
         end
        ) if_payment'=>'是否支付',
        '(case bill_type
            when 2 then "维修账单"
            when 3 then "维修账单"
            else (case front_type
                    when 1 then "车位账单"
                    when 7 then "车位账单"
                    else "物业账单"
                end )
         end
        ) bill_type'
            =>
            '账单类型',
        'remark'=>'备注',
        'created_at'=>'创建时间',
    ];
    public function __construct($request)
    {
        $this->request = $request;
    }

    /**
     * 加上表头
     * @param $list
     * @return array
     */
    public function addHeader($list){
        $header = [array_values($this->column)];
        return array_merge($header,$list);
    }

    /**
     * 拿到列
     */
    public function getColumn(){
        $header = [array_keys($this->column)];
        return implode(',',$header[0]);
    }

    /**
     * @return array
     */
    public function array(): array
    {
        if (is_array($this->request->ids)){
            $idsStr = implode(',',$this->request->ids);// 所有的id
        }else{
            $idsStr = $this->request->ids;// 所有的id
        }
        $inFeeBillController = new InFeeBillController();
        $inFeeBillController->request = $this->request;
        $inFeeBillController->model->column = $this->getColumn();
        Globals::$orderBy = "id";
        $inFeeBillController->model->sqlSearch($this->request,'id','in',$idsStr);
        $list = $inFeeBillController->model->list_(-1,-1);
        return $this->addHeader($list);
    }
}
